package com.baozhun.mall.common.util

import android.net.Uri
import android.util.Base64
import com.alibaba.android.arouter.launcher.ARouter
import com.baozhun.mall.common.R
import com.baozhun.mall.common.arouter.RoutePath
import com.baozhun.mall.common.base.BaseApplication
import com.baozhun.mall.common.base.mEventViewModel
import com.baozhun.mall.common.model.bean.ClassifyInfo
import com.baozhun.mall.common.model.enums.MainNavigateType
import com.orhanobut.logger.Logger

/**
 * @Description: 推送跟Scheme跳转工具栏
 * @Author: xuefeng.zhu
 * @CreateDate: 2020-08-18
 * @Version: 1.0
 */
object SchemeUtils {

    /**
     * 支持到首页，分类页，自营商品列表，商品详情（自营、淘宝、京东、拼多多）
     *
     * 首页=>houjimall://home
     * 分类页=>houjimall://catGoodsList?id=3&name=鞋子&goodsType=0
     * 余额兑换专区=>houjimall://exchangeZone
     * 商品详情=>houjimall://goodsDetail?goodsId=1363959&source=1
     * 活动页=>houjimall://activity?url=base64.encode后活动连接&title=活动标题
     *
     * 解析scheme数据，传入scheme uri[data]
     *
     * ps:
     * goodsType:0渠道，1自营
     * source:0淘宝，1天猫；2京东; 淘宝特价版;99 自营
     */
    fun parseScheme(data: Uri?) {
        if (data != null) {
            //解析data
            val scheme = data.scheme
            val host = data.host
            Logger.e("SchemeUtils--$scheme--host--$host")
            //获取指定参数
            if (BaseApplication.mInstance.getString(com.baozhun.mall.res.R.string.app_scheme) == scheme) {
                when (host) {
                    Constants.SCHEME.HOME -> { //首页
                        ARouter.getInstance().build(RoutePath.Main.MAIN).navigation()
                        mEventViewModel.switchTabEvent.value = MainNavigateType.values().indexOf(MainNavigateType.HOME)
                    }

                    Constants.SCHEME.CAT_GOODS_LIST -> { //分类页
                        var catId = data.getQueryParameter("id").toString()
                        var name = data.getQueryParameter("name").toString()
                        var goodsType = data.getQueryParameter("goodsType").toString() //0渠道，1自营

                        var classifyInfo = ClassifyInfo(catId, name, "", "", "", "", arrayListOf()).apply {
                            goods_type = goodsType
                        }
                        ARouter.getInstance().build(RoutePath.Home.CATEGORY_GOODS_LIST)
                            .withSerializable(Constants.Common.BUNDLE_NAME, classifyInfo)
                            .navigation()
                    }

                    Constants.SCHEME.EXCHANGE_ZONE -> { //自营商品列表
                        ARouter.getInstance().build(RoutePath.Home.INTEGRAL_EXCHANGE_ZONE_V2).navigation()
                    }

                    Constants.SCHEME.GOODS_DETAIL -> { //商品详情页面
                        var goodsId = data.getQueryParameter("goodsId")
                        var source = data.getQueryParameter("source")
                        ARouter.getInstance().build(RoutePath.Home.GOODS_DETAIL)
                            .withString(Constants.Goods.GOODS_ID, goodsId)
                            .withString(Constants.Goods.GOODS_SOURCE, source)
                            .navigation()
                    }

                    Constants.SCHEME.WAP -> { //H5页面
                        var url = data.getQueryParameter("url")
                        var title = data.getQueryParameter("title")
                        try {
                            url = Base64.decode(url, Base64.DEFAULT).decodeToString()
                        } catch (e: Exception) {
                            Logger.e(e.toString())
                        }
                        ARouter.getInstance().build(RoutePath.Common.WEB_CONTAINER)
                            .withString(Constants.Common.BUNDLE_NAME, url)
                            .withString(Constants.Common.TITLE, title)
                            .navigation()
                    }
                }
            }
        }
    }
}